{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数input()\n",
    "- 让程序暂停，等待用户输入一些文本。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tell me something,and I will repeat it back to you:hello world\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "massage = input(\"Tell me something,and I will repeat it back to you:\")\n",
    "print(massage)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 使用int()获取输入的数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "how old are you?22\n"
     ]
    }
   ],
   "source": [
    "age = input(\"how old are you?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# age->string\n",
    "age = int(age)\n",
    "age >= 18"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "current_number = 1\n",
    "while current_number <= 5:\n",
    "    print(current_number)\n",
    "    current_number += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 使用break推出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Please enter the name of a city you have visited:\n",
      "(Enter 'quit' when you are finished.) New York\n",
      "I‘d love to go to New York\n",
      "\n",
      "Please enter the name of a city you have visited:\n",
      "(Enter 'quit' when you are finished.) San Francisco\n",
      "I‘d love to go to San Francisco\n",
      "\n",
      "Please enter the name of a city you have visited:\n",
      "(Enter 'quit' when you are finished.) quit\n"
     ]
    }
   ],
   "source": [
    "prompt = \"\\nPlease enter the name of a city you have visited:\"\n",
    "prompt += \"\\n(Enter 'quit' when you are finished.) \"\n",
    "while True:\n",
    "    city = input(prompt)\n",
    "    #  一直到输入quit才终止\n",
    "    if city == 'quit':\n",
    "        break\n",
    "    else:\n",
    "        print(\"I‘d love to go to \" + city.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 在循环中使用continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "current_number = 0\n",
    "while current_number < 10:\n",
    "    current_number += 1\n",
    "    # 只要是偶数就跳过当前的循环 进行下一次的循环\n",
    "    if current_number % 2 == 0:\n",
    "        continue\n",
    "    \n",
    "    print(current_number)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用while循环来处理列表和字典\n",
    "- 在列表之间移动元素\n",
    "- 删除包含特定值的所有列表元素\n",
    "- 使用用户输入来填充字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "verifying user:Candace\n",
      "verifying user:Brian\n",
      "verifying user:Alice\n",
      "\n",
      "The following users have been confirmed:\n",
      "Candace\n",
      "Brian\n",
      "Alice\n"
     ]
    }
   ],
   "source": [
    "#在列表之间移动元素\n",
    "unconfirmed_users = ['alice','brian','candace']\n",
    "confirmed_users = []\n",
    "\n",
    "while unconfirmed_users:\n",
    "    current_user = unconfirmed_users.pop()\n",
    "    print(\"verifying user:\" + current_user.title())\n",
    "    confirmed_users.append(current_user)\n",
    "\n",
    "# 显示所有已经验证的用户\n",
    "print(\"\\nThe following users have been confirmed:\")\n",
    "for confirmed_user in confirmed_users:\n",
    "    print(confirmed_user.title())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']\n",
      "['dog', 'dog', 'goldfish', 'rabbit']\n"
     ]
    }
   ],
   "source": [
    "# 删除包含特定值的所有列表元素\n",
    "pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']\n",
    "print(pets)\n",
    "\n",
    "while 'cat' in pets:\n",
    "    pets.remove('cat')\n",
    "    \n",
    "print(pets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "what is your name?a\n",
      "which mountain would you like to climb someday?yes\n",
      "would you like to let another person respond(yes/no))yes\n",
      "\n",
      "what is your name?b\n",
      "which mountain would you like to climb someday?no\n",
      "would you like to let another person respond(yes/no))no\n",
      "\n",
      "---Poll Results---\n",
      "awould like to climb yes.\n",
      "bwould like to climb no.\n"
     ]
    }
   ],
   "source": [
    "# 使用用户输入来填充字典\n",
    "responses = {}\n",
    "\n",
    "polling_active = True\n",
    "while polling_active:\n",
    "    name = input(\"\\nwhat is your name?\")\n",
    "    response = input(\"which mountain would you like to climb someday?\")\n",
    "    \n",
    "    responses[name] = response\n",
    "    \n",
    "    repeat = input(\"would you like to let another person respond(yes/no))\")\n",
    "    if repeat == 'no':\n",
    "        polling_active = False\n",
    "        \n",
    "print(\"\\n---Poll Results---\")\n",
    "for name,response in responses.items():\n",
    "    print(name + \"would like to climb \" + response + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
